package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.BatchQueryEvaFillRoleListByCodesReq;
import com.sg.dto.base.app.req.BatchQueryEvaFillRoleReq;
import com.sg.dto.base.app.req.QueryEvaFillRoleDetailReq;
import com.sg.dto.base.app.req.QueryEvaFillRoleListReq;
import com.sg.entity.OmsEvaluationFillRole;
import com.sg.service.base.app.MOmsEvaluationFillRoleService;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.OmsEvaluationFillRoleMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsEvaluationFillRoleServiceImpl
        extends ServiceImpl<OmsEvaluationFillRoleMapper, OmsEvaluationFillRole>
        implements MOmsEvaluationFillRoleService {

    @Autowired
    OmsEvaluationFillRoleMapper omsEvaluationFillRoleMapper;

    /**
     * 3-2-06新增评价填报角色[2449]
     * gen by moon at 1/28/2023, 11:09:41 PM
     */
    @Trace(operationName = "3-2-06新增评价填报角色")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addEvaFillRole(OmsEvaluationFillRole omsEvaluationFillRole) {
        omsEvaluationFillRole.setEvaluationFillRoleId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsEvaluationFillRole.setBatchEndtTime(CommonFunctionHelper.getFutureTime());//FUTURE_CURRENT_TIME
        omsEvaluationFillRole.setIsUpdate("TRUE");
        omsEvaluationFillRole.setUpdateStatus("NEW");
        omsEvaluationFillRole.setArchivngType("NOT_ARCHIVE");
        omsEvaluationFillRole.setIsEditing("TRUE");
        omsEvaluationFillRole.setIsArchive("FALSE");
        omsEvaluationFillRole.setSubjectLifeCycle("EDITING");
        omsEvaluationFillRole.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsEvaluationFillRole.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsEvaluationFillRole.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsEvaluationFillRole.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsEvaluationFillRole.setIsValid("FALSE");
        omsEvaluationFillRole.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsEvaluationFillRole.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsEvaluationFillRole)) {
            return omsEvaluationFillRole.getEvaluationFillRoleId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-2-06查评价填报角色详情[2380]
     * gen by moon at 3/30/2023, 6:54:23 PM
     */
    @Trace(operationName = "3-2-06查评价填报角色详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsEvaluationFillRole queryEvaFillRoleDetail(QueryEvaFillRoleDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            QueryWrapper<OmsEvaluationFillRole> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsEvaluationFillRole> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsEvaluationFillRole retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-2-06查评价填报角色列表[2369]
     * gen by moon at 10/15/2022, 5:44:19 PM
     */
    @Trace(operationName = "3-2-06查评价填报角色列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsEvaluationFillRole> queryEvaFillRoleList(QueryEvaFillRoleListReq reqDto) {

        try {
            QueryWrapper<OmsEvaluationFillRole> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsEvaluationFillRole.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsEvaluationFillRole.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsEvaluationFillRole.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsEvaluationFillRole>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-2-06批量增评价填报角色[3217]
     * gen by moon at 1/28/2023, 11:09:42 PM
     */
    @Trace(operationName = "3-2-06批量增评价填报角色")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchAddEvaFillRole(List<OmsEvaluationFillRole> omsEvaluationFillRole) {

        if (omsEvaluationFillRole == null || omsEvaluationFillRole.size() == 0) {
            return false;
        }

        return super.saveBatch(omsEvaluationFillRole.stream().map(item -> {
            item.setEvaluationFillRoleId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
            item.setBatchEndtTime(CommonFunctionHelper.getFutureTime());//FUTURE_CURRENT_TIME
            item.setIsUpdate("TRUE");
            item.setUpdateStatus("NEW");
            item.setArchivngType("NOT_ARCHIVE");
            item.setIsEditing("TRUE");
            item.setIsArchive("FALSE");
            item.setSubjectLifeCycle("EDITING");
            item.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            item.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            item.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            item.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            item.setIsValid("FALSE");
            item.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            item.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
            return item;
        }).collect(Collectors.toList()));
    }

    /**
     * 3-2-06查询评价填报角色列表ByCodes[3230]
     * gen by moon at 10/15/2022, 5:44:21 PM
     */
    @Trace(operationName = "3-2-06查询评价填报角色列表ByCodes")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsEvaluationFillRole> batchQueryEvaFillRoleListByCodes(BatchQueryEvaFillRoleListByCodesReq reqDto) {

        try {
            QueryWrapper<OmsEvaluationFillRole> queryWrapper = new QueryWrapper<>();

            if (reqDto.getEvaFillRoleList() == null || reqDto.getEvaFillRoleList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("role_code", reqDto.getEvaFillRoleList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsEvaluationFillRole.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsEvaluationFillRole.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsEvaluationFillRole.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsEvaluationFillRole>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-2-06批量删评价填报角色[4213]
     * gen by moon at 10/15/2022, 5:44:22 PM
     */
    @Trace(operationName = "3-2-06批量删评价填报角色")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteEvaFillRole(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 3-2-06批量查询评价填报角色[5501]
     * gen by moon at 12/19/2022, 5:32:51 AM
     */
    @Trace(operationName = "3-2-06批量查询评价填报角色")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsEvaluationFillRole> batchQueryEvaFillRole(BatchQueryEvaFillRoleReq reqDto) {

        try {
            QueryWrapper<OmsEvaluationFillRole> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            if (reqDto.getEvaFillRoleList() == null || reqDto.getEvaFillRoleList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("evaluation_fill_role_id", reqDto.getEvaFillRoleList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsEvaluationFillRole.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsEvaluationFillRole.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsEvaluationFillRole.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsEvaluationFillRole>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-2-06批量改评价填报角色[7175]
     * gen by moon at 6/9/2023, 8:40:54 PM
     */
    @Trace(operationName = "3-2-06批量改评价填报角色")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateEvaFillRole(List<OmsEvaluationFillRole> omsEvaluationFillRole) {

        if (omsEvaluationFillRole == null || omsEvaluationFillRole.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(omsEvaluationFillRole);

    }
}
